package com.webamc.manager;

import com.webamc.common.enums.Fields;
import com.webamc.common.enums.Params;
import com.webamc.dao.IAMCTaskDao;
import com.webamc.model.AMCTask;
import com.webamc.util.CommonUtil;
import com.webamc.util.KeyUtil;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;

/**
 * Date: Mar 8, 2009
 * Time: 3:06:33 AM
 */
public class AMCTaskHandler implements IAMCTaskHandler {
    private Logger log = Logger.getLogger(AMCTaskHandler.class);

    private IAMCTaskDao amcTaskDao;

    public void setAmcTaskDao(IAMCTaskDao amcTaskDao) {
        this.amcTaskDao = amcTaskDao;
    }

    public boolean create(AMCTask u) {
        log.info("creating AMCTask = " + u);
        u.setAmcTaskId(KeyUtil.instance().getNextAmcTaskId());
        return amcTaskDao.create(u);
    }

    public boolean update(AMCTask u) {
        log.info("updating AMCTask = " + u);
        return amcTaskDao.update(u);
    }

    public AMCTask view(int id) {
        log.info("checking AMCTaskId= " + id);
        AMCTask a = create();
        a.setAmcTaskId(id);
        List<AMCTask> l = search(a);
        return l.size() > 0 ? l.get(0) : null;
    }

    public List<AMCTask> search(AMCTask u) {
        log.info("searching AMCTask= " + u);
        return amcTaskDao.search(u);
    }

    public void update(Map<String, String[]> m, AMCTask o) {
        o.setStartTime(CommonUtil.getTime(m, Fields.STARTTIME, o.getStartTime()));
        o.setComments(m.get(Fields.COMMENTS));
        o.setWorklogs(m.get(Fields.WORKLOGS));
        o.setDescription(CommonUtil.getString(m, Fields.DESCRIPTION, o.getDescription()));
        o.setAssigneeId(CommonUtil.getInt(m, Fields.ASSIGNEEID, o.getAssigneeId()));
        o.setSupervisorId(CommonUtil.getInt(m, Fields.SUPERVISORID, o.getSupervisorId()));
        o.setAdhoc(CommonUtil.getBool(m, Fields.ISADHOC, o.isAdhoc()));
        o.setCategory(CommonUtil.getString(m, Fields.AMCTASKCATEGORY, o.getCategory()));
        o.setStatus(CommonUtil.getString(m, Fields.AMCTASKSTATUS, o.getStatus()));
    }

    public AMCTask create() {
        return new AMCTask();
    }

    public String validate(AMCTask u) {
        return Params.VALID;
    }

    public AMCTask update(Map<String, String[]> m, int userId) {
        int id = CommonUtil.getInt(m, Fields.AMCTASKID, 0);

        AMCTask p = view(id);
        update(m, p); //Param Validation
        validate(p);
        p.setVersion(p.getVersion() + 1);
        p.setLastUpdatedById(userId);
        p.setLastUpdated(System.currentTimeMillis());
        update(p);

        return p;
    }

    public AMCTask create(Map<String, String[]> m, int userId) {
        AMCTask p = create();
        validate(p);
        update(m, p); //Param Validation
        p.setAmcOrderId(CommonUtil.getInt(m, Fields.AMCORDERID, 0)); //one time
        p.setCreationTime(System.currentTimeMillis()); //one time
        p.setVersion(1);
        p.setLastUpdatedById(userId);
        p.setLastUpdated(System.currentTimeMillis());
        create(p);

        return p;
    }

    public List<AMCTask> search(Map<String, String[]> m) {
        AMCTask u = create();
        u.setAmcTaskId(CommonUtil.getInt(m, Fields.AMCTASKID, u.getAmcTaskId()));
        update(m, u);

        return search(u);
    }
}